<template>
    <div class="box">
        <div class="header">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>场馆维护</el-breadcrumb-item>
                <el-breadcrumb-item><a href="#">场地共享</a></el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="demo-collapse">
            <el-collapse accordion>
                <el-collapse-item :name="item.id" v-for="item in file" :key="item.id">
                    <template #title>
                        <div class="tag">场馆</div>
                        {{ item.name }}
                        <button class="shareBtn" @click="Add(item.fields[0].sport_id)">新增共享</button>
                    </template>
                    <div class="shareList">
                        <div class="title">共享场地列表</div>
                        <div class="tableList">
                            <table>
                                <tr>
                                    <td>项目</td>
                                    <td>场地</td>
                                    <td>共享项目</td>
                                    <td>场地名称</td>
                                    <td>状态</td>
                                    <td>操作</td>
                                </tr>
                                <tr v-for="i in item.fields" :key="i.id">
                                    <td>{{ i.sport_type_codeName }}</td>
                                    <td>{{ i.sport_name }}</td>
                                    <td>{{ i.sport_type_codeName }}</td>
                                    <td>
                                        <span>{{ i.name }}</span>
                                    </td>
                                    <td>共享</td>
                                    <td>
                                        <button @click="toEdit(i)">修改</button>
                                        <button @click="Del(i)">删除</button>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </el-collapse-item>
            </el-collapse>
        </div>
        <!-- 新增弹框 -->
        <el-dialog v-model="showAdd" title="添加/修改共享信息" width="30%">
            
            <div class="select_add">
                <el-form :model="Addlist" class="addList" label-width="80">
                    <el-form-item label="项目名称">
                         <el-input placeholder="请输入项目名称" v-model="Addlist.sport_type_codeName" />
                    </el-form-item>       
                </el-form>
            </div>
            <div class="select_add">
                <span>场地</span>  
                <el-select v-model="Addlist.sport_name" placeholder="请选择场地名称" class="selsect_season">
                    <el-option :label="item.name" :value="item.name" v-for="item in file" :key="item.id"/>
                </el-select>  
            </div>
            <el-form :model="addForm" class="addForm" label-width="80">
                <el-form-item label="共享类型">
                    <el-input v-model="Addlist.type" placeholder="共享类型" disabled="true"/>
                </el-form-item>
                <el-form-item label="共享场地名称">
                    <el-input v-model="Addlist.name" placeholder="共享场地名称" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="updata" v-if="showEdit">修改</el-button>
                    <el-button type="primary" @click="submit" v-else>保存</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script  setup>
// import { InfoFilled } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { GetshareField, AddshareField, DelshareField, upDateshare } from '../../../../api/Myapi'
import { ElMessage, ElMessageBox } from 'element-plus'
const file = ref([])
//新增弹出框
const showAdd = ref(false)
// 定义是否显示编辑
const showEdit = ref(false);
//新增共享
const Addlist = ref({
    id: '',
    type: 2,
    name: '',
    sport_id: '',
    disable: 0,
    is_del: 1,
    sport_name: '',
    sport_type_codeName: '',
    sharename: '',
    company_id: 1
})
//点击新增
const Add = (id) => {
    Addlist.value = {
        id: '',
        type: 2,
        name: '',
        sport_id: '',
        disable: 0,
        is_del: 1,
        sport_name: '',
        sport_type_codeName: '',
        sharename: '',
        company_id: 1
    }
    showAdd.value = true
    showEdit.value = false
    Addlist.value.sport_id = id
    console.log(id);
}
//获取共享场地
const getShare = () => {
    GetshareField({ order:'desc' }).then(res => {
        if (res.data.code == 0) {
            file.value = res.data.data
            res.data.data.forEach(e => {
                e.fields.forEach(i=>{
                    Addlist.value.sport_type_codeName=i.sport_type_codeName
                    Addlist.value.sport_name=i.sport_name 
                })
            })
            
        }
    })
}
getShare()
//添加共享
const submit = () => {
    showAdd.value = false;
    console.log(Addlist.value);
    AddshareField(Addlist.value).then(res => {
        console.log(res);
        getShare()
        Addlist.value = {
            id: '',
            type: 2,
            name: '',
            sport_id: '',
            disable: 0,
            is_del: 1,
            sport_name: '',
            sport_type_codeName: '',
            sharename: '',
            company_id: 1
        }
    })
}
//删除
const Del = (i) => {
    console.log(i);
    ElMessageBox.confirm('删除不可逆', '确认删除',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        })
        .then(() => {
            DelshareField(i.id).then(res => {
                if (res.data.code == 0) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                    getShare()
                } else {
                    ElMessage({
                        type: 'error',
                        message: '删除失败',
                    })
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '删除取消',
            })
        })
}
//修改
const toEdit = (item) => {
    showAdd.value = true
    showEdit.value = true
    Addlist.value = { ...item }
}
// 编辑共享
const updata = () => {
    upDateshare(Addlist.value).then(res => {
        if (res.data.code == 0) {
            ElMessage({
                type: 'success',
                message: '修改成功',
            })
            getShare()
            showAdd.value = false
            Addlist.value = {
                id: '',
                type: 2,
                name: '',
                sport_id: '',
                disable: 0,
                is_del: 1,
                sport_name: '',
                sport_type_codeName: '',
                sharename: '',
                company_id: 1
            }
        }
    })
}
</script>
<style lang="scss" scoped>
.box {
    margin: 0;
    padding: 0;

    .header {
        width: 100%;
        height: 45px;
        background-color: rgb(245, 245, 245);
        padding-left: 50px;

        a {
            font-size: 13px;
        }

        span {
            line-height: 45px;
        }
    }

    .demo-collapse {
        width: 90%;
        margin: 0 auto;

        .tag {
            width: 30px;
            height: 30px;
            background-color: rgb(236, 166, 25);
            border-radius: 50%;
            transform: scale(0.6);
            font-size: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            color: white;
        }

        .shareBtn {
            border: none;
            background-color: rgb(165, 208, 241);
            font-size: 12px;
            padding: 3px 6px;
            color: white;
            border-radius: 6px;
            margin: 30px;

        }

        .title {
            height: 50px;
            line-height: 50px;
        }

        .tableList {

            table {
                width: 95%;
                margin: 0 auto;
                border-collapse: collapse;
                text-align: center;

                tr {
                    border-bottom: 1px solid #ccc;
                    height: 40px;

                    &:nth-of-type(1) {
                        background-color: rgb(245, 245, 245);
                        border: none;
                    }

                    td {
                        button {
                            width: 55px;
                            border: none;
                            margin-right: 10px;
                            border-radius: 6px;
                            padding: 2px;
                            font-size: 12px;
                            color: white;

                            &:nth-of-type(1) {
                                background-color: rgb(104, 204, 220);
                            }

                            &:nth-of-type(2) {
                                background-color: rgb(234, 96, 83);
                            }
                        }
                    }
                }
            }
        }
    }
    .select_add{
        margin: 10px auto;
        width: 280px;
        display: flex;
        justify-content: space-between;
        span{
            text-align: right;
            width: 60px;
            display: inline-block;
        }
    }
    .selsect_season{
        width: 200px;
        
    }
    .addForm{
        width: 280px;
        margin: 10px auto;
    }
}
</style>